package me.huanmeng.heater.heather;

import lombok.Getter;
import me.huanmeng.heater.Heater;
import me.huanmeng.heater.command.Command;
import me.huanmeng.heater.command.manager.CommandManager;
import me.huanmeng.heater.event.Event;
import me.huanmeng.heater.event.EventBus;
import me.huanmeng.heater.object.ChannelInfo;
import me.huanmeng.heater.object.Guild;
import me.huanmeng.heater.object.UserInfo;
import me.huanmeng.heater.plugin.Plugin;
import me.huanmeng.heater.sender.Channel;
import me.huanmeng.heater.sender.CommandSender;
import me.huanmeng.heater.sender.User;
import me.huanmeng.heater.sender.manager.ChannelManager;
import me.huanmeng.heater.sender.manager.GuildManager;
import me.huanmeng.heater.sender.manager.UserManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

/**
 * 2021/10/1<br>
 * Heater<br>
 *
 * @author huanmeng_qwq
 */
public class ServerImpl implements Server {
    private final GuildManager guildManager;
    private final ChannelManager channelManager;
    private final UserManager userManager;
    private final CommandManager commandManager;
    @Getter
    private final EventBus eventBus;

    public ServerImpl() {
        Heater.setServer(this);
        eventBus = new EventBus();
        guildManager=new GuildManager();
        channelManager = new ChannelManager();
        userManager = new UserManager();
        commandManager = new CommandManager();
    }

    @Override
    public String getName() {
        return "Heater";
    }

    @Override
    public String getVersion() {
        return "1.0";
    }

    @Override
    public <T extends Event> @NotNull T callEvent(@NotNull T event) {
        return eventBus.callEvent(event);
    }

    @Override
    public void registerEvents(Plugin plugin, @NotNull Object listener) {
        eventBus.registerEvent(plugin, listener);
    }

    @Override
    public Channel getChannel(@NotNull String id) {
        return channelManager.getChannel(id);
    }

    @Override
    public void setChannel(@NotNull ChannelInfo channel) {
        channelManager.updateChannelInfo(channel);
    }

    @Override
    public User getUser(@NotNull String id) {
        return userManager.getUser(id);
    }

    @Override
    public void setUser(@NotNull UserInfo userInfo) {
        userManager.updateUserInfo(userInfo);
    }

    @Override
    public void executeCommand(@NotNull CommandSender sender, @Nullable Channel channel, @NotNull String line) {
        commandManager.executeCommand(sender, channel, line);
    }

    @Override
    public void registerCommand(@NotNull Plugin plugin, @NotNull Command command) {
        commandManager.register(plugin, command);
    }

    @Override
    public Guild getGuild(String id) {
        return guildManager.getGuild(id);
    }

    @Override
    public void setGuild(Guild guild) {
        guildManager.update(guild);
    }

    @Override
    public Collection<Guild> getGuilds() {
        return guildManager.getGuilds();
    }

    @Override
    public UserInfo getUserInfo(String id) {
        return userManager.getUserInfo(id);
    }

    /**
     * 判断当前服务器版本是否支持
     *
     * @param api api
     * @return 是否支持
     */
    public boolean allowSupport(Collection<String> api) {
        for (String s : api) {
            if (ServerVersion.getFrom(s) != null) {
                return true;
            }
        }
        return false;
    }
}
